package com.kaiyidiannao.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.kaiyidiannao.main.R;
import com.kaiyidiannao.model.bean.DaydayCouponBean;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by 大志 on 14-4-4.
 * 天天惠列表页面Adaper
 */
public class DaydaycouponListAdapter extends ImageLoaderBaseAdapter {

    public static final String itemid = 11 +"";

    ImageView daydaycoupon_favorites; // 点赞的按钮

    private Context context;
    private LayoutInflater layoutInflater;
    private List<?> list = new ArrayList<DaydayCouponBean>();

    public DaydaycouponListAdapter(Context context, List<?> list) {

        this.context  = context;
        imageLoader = ImageLoader.getInstance();
        imageLoader.init(ImageLoaderConfiguration.createDefault(context));
        this.list = list;
        this.layoutInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView thumeImageView = null;//缩略图
        TextView titleTextView = null;//标题
        TextView priceTextView = null;//价格
        TextView salesTextView = null;//销售量

        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.daydaycoupon_list_item, null);
        }

        DaydayCouponBean bean = null;
        try {
            bean = (DaydayCouponBean) list.get(position);
            thumeImageView = (ImageView) convertView.findViewById(R.id.daydaycoupon_thumb);
            titleTextView = (TextView) convertView.findViewById(R.id.daydaycoupon_title);
            priceTextView = (TextView) convertView.findViewById(R.id.daydaycoupon_price);
            salesTextView = (TextView) convertView.findViewById(R.id.daydaycoupon_sales);
            daydaycoupon_favorites = (ImageView) convertView.findViewById(R.id.daydaycoupon_favorites);
            daydaycoupon_favorites.setVisibility(View.INVISIBLE);

            salesTextView.setText(bean.getSales());
            imageLoader.displayImage(bean.getThumb(), thumeImageView, options, animateFirstListener);
            titleTextView.setText(bean.getTitle());
            priceTextView.setText(bean.getPrice());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return convertView;
    }

    private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

        static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            if (loadedImage != null) {
                ImageView imageView = (ImageView) view;
                boolean firstDisplay = !displayedImages.contains(imageUri);
                if (firstDisplay) {
                    FadeInBitmapDisplayer.animate(imageView, 500);
                    displayedImages.add(imageUri);
                }
            }
        }
    }
}
